#include "TLabel.h"
#include "../utils/Utils.h"
#define DEFAULT_WIDTH 100
#define DEFAULT_HEIGHT 21 
#define DEFAULT_FONT_SIZE 12

TextFormat::TextFormat( int nWidth,
                         int nHeight,
                         int nFontSize,
                         ccColor3B color ,
                         CCTextAlignment alignH,
                         CCVerticalTextAlignment alignV){
    
    this->nWidth=nWidth;
    this->nHeight=nHeight;
    this->nFontSize=nFontSize;
    this->color=color;
    this->alignH=alignH;
    this->alignV=alignV;
    
    
}


TLabel::TLabel(){
    m_pLabel=NULL;
}
TLabel::~TLabel(){
    
}


CCTextAlignment getHAlign(string name){ 
    if(name=="center"){
        return kCCTextAlignmentCenter;
    }else if(name=="right"){
        return kCCTextAlignmentRight;
    }else{
        return kCCTextAlignmentLeft;
    }
}


CCVerticalTextAlignment getVAlign(string name){ 
    if(name=="middle"){
        return kCCVerticalTextAlignmentCenter;
    }else if(name=="top"){
        return kCCVerticalTextAlignmentTop;
    }else
    {
        return kCCVerticalTextAlignmentBottom;
    }
}


void TLabel::setString(int num){
    setString(parseString(num));
}

void TLabel::setString(const string & str){
    setString(str.c_str());
}

void TLabel::setString(const char* param){
    if(param==NULL){
        param="";
    }
    
     
    std::string str=m_pLabel->getString();
    
    m_pLabel->setString(param);
    setContentSize(m_pLabel->getContentSize());
    m_pLabel->setPositionX(getContentSize().width/2);
    m_pLabel->setPositionY(-getContentSize().height/2);
    
    if (str.compare(param))
    { 
        onStringChange();
    }
    
        
}

void TLabel::onStringChange(){
    
}

const char* TLabel::getString(){
    return m_pLabel->getString();
}


int TLabel::getNum(){
    string ret=getString();
    if(ret.size()<=0){
        return 0;
        
    }
    
    return parseInt(ret);
}

void TLabel::setColor(const ccColor3B& color){
    m_pLabel->setColor(color);
}

TLabel* TLabel::create(xml_node<> * pItem){

    TLabel*  ret=TLabel::create();
    ret->initWidthConf(pItem);
    return ret;
}

void TLabel::initWidthFormat(const TextFormat& format,const char* txt){
    if(txt==NULL){
        txt="";
    }
    m_pLabel= CCLabelTTF::create(txt,
                                "Helvetica",
                                format.nFontSize,
                                CCSizeMake(format.nWidth,format.nHeight),
                                format.alignH,
                                format.alignV);
    
    m_pLabel->setColor(format.color); 
    addChild(m_pLabel); 
     
    setString(txt);
}

TLabel* TLabel::create(const TextFormat& format,const char* txt){
     TLabel*  ret=TLabel::create();
     ret->initWidthFormat(format,txt);
     return ret;
}

void TLabel::initWidthConf(xml_node<> * pItem){
    int width,height,fontSize;
    width= readAttrInt(pItem, "width");
    height= readAttrInt(pItem, "height");
    fontSize= readAttrInt(pItem, "fontSize");
    
    
    int nowWidth=DEFAULT_WIDTH;
    int nowHeight=DEFAULT_HEIGHT;
    int nowFontSize=DEFAULT_FONT_SIZE;
    CCVerticalTextAlignment vAlign=kCCVerticalTextAlignmentTop;
    CCTextAlignment hAlign=kCCTextAlignmentCenter;
    
    
    string strVA="",strHA="" ,color;
    readAttrString(pItem, "textAlign", strHA);
    readAttrString(pItem, "verticalAlign", strVA);
    
    readAttrString(pItem, "color", color);
    
   
    
    
    
    
    if(strHA.size()!=0){
        hAlign=getHAlign(strHA);
    }else{
        hAlign=kCCTextAlignmentCenter;
    }
    
    if(strVA.size()!=0){
        vAlign=getVAlign(strVA);
    }else{
        vAlign=kCCVerticalTextAlignmentCenter;
    }
    
    if(width!=0){
        nowWidth=width;
    }
    
    if(height!=0){
        nowHeight=height;
    }
    if(fontSize!=0){
        nowFontSize=fontSize;
    }
    string text;
    readAttrString(pItem, "text", text);
    
    m_pLabel=CCLabelTTF::create(text.c_str(),"Helvetica", nowFontSize,CCSizeMake(nowWidth, nowHeight),hAlign,vAlign);
    addChild(m_pLabel);
    
    if(color.size()!=0){
        replaceStr(color, "#", "");
        int temp=HexToDec(color.c_str());
        m_pLabel->setColor(parseRgb(temp));
    }
    
    
    
  
    m_pLabel->setPositionX(nowWidth/2);
    m_pLabel->setPositionY(-nowHeight/2); 
    
    setContentSize(CCSizeMake(nowWidth, nowHeight));
    onStringChange();
}



void TLabel::setHorizontalAlignment(CCTextAlignment align){
    if(m_pLabel)
    {
        m_pLabel->setHorizontalAlignment(align);
    }
}
void TLabel::setVerticalAlignment(CCVerticalTextAlignment align) {
    if(m_pLabel)
    {
        m_pLabel->setVerticalAlignment(align);
    }
}







void TInput::editBoxEditingDidBegin(CCEditBox* editBox){
    
}
void TInput::editBoxEditingDidEnd(CCEditBox* editBox){
    
}
void TInput::editBoxTextChanged(CCEditBox* editBox, const std::string& text){
    
}

void TInput::editBoxReturn(CCEditBox* editBox) {
    
}

TInput* TInput::create(xml_node<> * pItem){
    
    TInput*  ret=TInput::create();
    ret->initWidthConf(pItem);
    return ret;
}

EditBoxInputFlag getInputFlag(int num){ 
    
    switch (num) { 
        case 0: return kEditBoxInputFlagPassword;  break;
        case 1: return kEditBoxInputFlagSensitive;  break;
        case 2: return kEditBoxInputFlagInitialCapsWord;  break;
        case 3: return kEditBoxInputFlagInitialCapsSentence;  break;
        case 4: return kEditBoxInputFlagInitialCapsAllCharacters;  break;
            
        default:
            break;
    }

    return kEditBoxInputFlagInitialCapsAllCharacters;
}


EditBoxInputMode getInputMode(int num){
    
    switch (num) {
        case 0: return kEditBoxInputModeAny;  break;
        case 1: return kEditBoxInputModeEmailAddr;  break;
        case 2: return kEditBoxInputModeNumeric;  break;
        case 3: return kEditBoxInputModePhoneNumber;  break;
        case 4: return kEditBoxInputModeUrl;  break;
        case 5: return kEditBoxInputModeDecimal;  break;
        case 6: return kEditBoxInputModeSingleLine;  break; 
            
        default:
            break;
    }
  
    return kEditBoxInputModeAny;
}


KeyboardReturnType getInputReturnType(int num){
    switch (num) {
        case 0: return kKeyboardReturnTypeDefault;  break;
        case 1: return kKeyboardReturnTypeDone;  break;
        case 2: return kKeyboardReturnTypeSend;  break;
        case 3: return kKeyboardReturnTypeSearch;  break;
        case 4: return kKeyboardReturnTypeGo;  break;
            
        default:
            break;
    }
    
    return kKeyboardReturnTypeDefault;
}


TInput::TInput(){
    m_pEditor=NULL;
}

bool TInput::init(){
    
    if(!TPanel::init()){
        return false;
    } 
    return true;
}


const char* TInput::getString(){
    if(m_pEditor){
        return m_pEditor->getText();
    }
    return "";
}

void TInput::setString(const char* str)
{
    if(str==NULL)
    {
        str="";
    }
    
    if(m_pEditor)
    {
        return m_pEditor->setText(str);
    }
}

void TInput::initWidthConf(xml_node<> * pItem){
    int width,height,fontSize,maxNum,returnType,inputMode,inputFlag;
    width= readAttrInt(pItem, "width");
    height= readAttrInt(pItem, "height");
    fontSize= readAttrInt(pItem, "fontSize");
    maxNum= readAttrInt(pItem, "maxNum");
    returnType= readAttrInt(pItem, "returnType");
    inputFlag= readAttrInt(pItem, "inputFlag");
    inputMode= readAttrInt(pItem, "inputMode");
     
    
    int nowWidth=DEFAULT_WIDTH;
    int nowHeight=DEFAULT_HEIGHT;
    int nowFontSize=DEFAULT_FONT_SIZE; 
    
    
    string color; 
    readAttrString(pItem, "color", color);
    
     
   
    if(width!=0){
        nowWidth=width;
    }
    
    if(height!=0){
        nowHeight=height;
    }
    if(fontSize!=0){
        nowFontSize=fontSize;
    }
    string text,bg;
    readAttrString(pItem, "text", text);
    readAttrString(pItem, "bg", bg);
    setContentSize(CCSizeMake(nowWidth, nowHeight));
    
    m_pEditor=CCEditBox::create(CCSizeMake(nowWidth, nowHeight),CCScale9Sprite::createWithSpriteFrameName(bg.c_str()));
    if(text.size()!=0){
        m_pEditor->setPlaceHolder(text.c_str());
    }
    if(color.size()!=0){
        replaceStr(color, "#", "");
        int temp=HexToDec(color.c_str());
        m_pEditor->setFontColor(parseRgb(temp));
    }
    m_pEditor->setFontName("Helvetica");
    m_pEditor->setFontSize(nowFontSize);
  
    m_pEditor->setTouchPriority(kCCMenuHandlerPriority);  
    m_pEditor->setMaxLength(maxNum);
    m_pEditor->setInputFlag(getInputFlag(inputFlag));
    m_pEditor->setInputMode(getInputMode(inputMode));
    m_pEditor->setReturnType(getInputReturnType(returnType));
 
    
    addChild(m_pEditor);
    
   
    m_pEditor->setPositionX(nowWidth/2);
    m_pEditor->setPositionY(-nowHeight/2);
}




CCLabelTTF*  createLabel(  const TextFormat& format,const char* label){
    
    CCLabelTTF* retLabel=  createLabel(format.nWidth,
                                       format.nHeight,
                                       format.nFontSize,
                                       format.color,
                                       format.alignH,
                                       format.alignV
                                       );
    
    retLabel->setString(label);
    return retLabel;
}

CCLabelTTF*  createLabel(int nWidth,
                         int nHeight,
                         int nFontSize,
                         ccColor3B color,
                         CCTextAlignment alignH,
                         CCVerticalTextAlignment alignV){
    CCLabelTTF* retLabel= CCLabelTTF::create("",
                                             "Thonburi",
                                             nFontSize,
                                             CCSizeMake(nWidth,nHeight),
                                             alignH,
                                             alignV);
    
    retLabel->setColor(color);
    return retLabel;
    
}

 




TStrokeLabel::TStrokeLabel(){
    m_pLabel=NULL;
    m_pStroke=NULL;
    strokeColor=ccc3(0,0,0);
    strokeSize=2.0f;
    strokeAlpha=255;
}


TStrokeLabel* TStrokeLabel::create(int nWidth,
                                     int nHeight,
                                     int nFontSize,
                                     ccColor3B color,
                                     CCTextAlignment alignH,
                                     CCVerticalTextAlignment alignV){
    
    
    TStrokeLabel* retLabel=new TStrokeLabel;
    retLabel->autorelease();
    retLabel->initStrokeLabel( nWidth,
                              nHeight,
                              nFontSize,
                              color,
                              alignH,
                              alignV);
    return retLabel;
}

void TStrokeLabel::initStrokeLabel(int nWidth,
                                    int nHeight,
                                    int nFontSize,
                                    ccColor3B color,
                                    CCTextAlignment alignH,
                                    CCVerticalTextAlignment alignV){
    
    if(m_pLabel &&m_pLabel->getParent()){
        m_pLabel->getParent()->removeChild(m_pLabel, true);
    }
    
    
    m_pLabel= createLabel(  nWidth,
                          nHeight,
                          nFontSize,
                          color,
                          alignH,
                          alignV);
    setContentSize(CCSizeMake(nWidth, nHeight));
    addChild(m_pLabel);
    
}

void TStrokeLabel::initStroke(){
    if(m_pStroke &&m_pStroke->getParent()){
        m_pStroke->getParent()->removeChild(m_pStroke, true);
    }
    
    m_pStroke = createStroke(m_pLabel,strokeSize, strokeColor, strokeAlpha ); 
    addChild(m_pStroke, m_pLabel->getZOrder() - 1);
    
}


void TStrokeLabel::onStringChange(){
    std::string str=m_pLabel->getString();
    
    if(str.size()==0){
        if(m_pStroke){ 
            m_pStroke->setVisible(false);
        }
    }else{
        
        initStroke();
    } 
}
 





void TStrokeLabel::initWidthConf(xml_node<> * pItem){
   
    string strokecolor="";
    strokeSize= readAttrInt(pItem, "strokeSize"); 
    readAttrString(pItem, "strokeColor", strokecolor);
     
    if(strokecolor.size()!=0){
        replaceStr(strokecolor, "#", "");
        int temp=HexToDec(strokecolor.c_str());
        strokeColor=(parseRgb(temp));
    } 
    
    TLabel::initWidthConf(pItem);
 
     
}






 
